package com.jt.controller;


import com.jt.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.*;

@RestController
@RequestMapping("test")
public class RedisController {

    @Autowired
    private RedisService redisService;

    @GetMapping("/lock")
    public void TestRedis(){


        ExecutorService pool = Executors.newScheduledThreadPool(3);
        for (int i = 0; i < 5; i++) {
            final int taskId = i;
            pool.execute(()->{
                System.out.println(Thread.currentThread().getName() + " 正在执行任务 " + taskId);
                try {
                    redisService.lockMethod();
                }catch (Exception e){
                }
            });
        }
        pool.shutdown();
    }

    public void  test(){
        // 自定义线程池
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                2, // 核心线程数
                5, // 最大线程数
                60, // 空闲线程存活时间
                TimeUnit.SECONDS, // 时间单位
                new ArrayBlockingQueue<>(10), // 任务队列
                Executors.defaultThreadFactory(), // 线程工厂
                new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略
        );
    }
}
